package top.lenjon.lib.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import top.lenjon.lib.entity.Log;

import java.util.List;

/**
 * <p>
 * 借阅记录 Mapper 接口
 * </p>
 *
 * @author wei
 * @since 2020-11-09
 */

@Repository
public interface LogMapper extends BaseMapper<Log> {
    /**
     * 获取所有记录
     * 编程规约是啥？我偏要select *
     *
     * @param borrowerId borrowerId
     * @return
     */
    @Select("SELECT * FROM log WHERE borrower_id=#{borrower_id}")
    List<Log> getAllLogOf(int borrowerId);

    /**
     * 获取未还记录
     * 编程规约是啥？我偏要select *
     *
     * @param borrowerId borrowerId
     * @return
     */
    @Select("SELECT * FROM log WHERE ISNULL(return_time) AND borrower_id=#{borrower_id}")
    List<Log> getNotReturnLogOf(int borrowerId);

    /**
     * 获取已还记录
     * 编程规约是啥？我偏要select *
     *
     * @param borrowerId borrowerId
     * @return
     */
    @Select("SELECT * FROM log WHERE NOT ISNULL(return_time) AND borrower_id=#{borrower_id}")
    List<Log> getReturnedLogOf(int borrowerId);

    @Select("SELECT * FROM log")
    List<Log> getAll();

    @Select("SELECT * FROM log WHERE ISNULL(return_time)")
    List<Log> getNoRe();

    @Select("SELECT * FROM log WHERE NOT ISNULL(return_time)")
    List<Log> geths();

    /**
     * 续借
     *
     * @param bookId
     */
    @Update("UPDATE log SET renew_times=renew_times+1 WHERE book_id=#{bookId}")
    void renew(int bookId);

    /**
     * 还书
     *
     * @param bookId
     */
    @Update("UPDATE log SET return_time=NOW() WHERE book_id=#{bookId}")
    void returnBook(int bookId);

    /**
     * 借书
     *
     * @param borrowerId
     * @param bookId
     */
    @Insert("INSERT INTO log(borrower_id,book_id) values(#{borrowerId}, #{book_id})")
    void borrowByUser(int borrowerId, int bookId);

}
